﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace SmartBot
{
    /// <summary>
    /// File partial class chứa tất cả các function cần thiết 
    /// </summary>
    public partial class SmartClass
    {

        /// <summary>
        /// This function read all information of the remote processes
        /// </summary>
        public void ReadAllMemory()
        {
            while (true)
            {
                if (UIAvailable == true)
                {
                    //Check if it's time to enum new target processes
                    if (Math.Abs(System.Environment.TickCount - GlobalSetting.EnumProcessTimeStamp) >= GlobalSetting.EnumProcessDelay)
                    {
                        //Save new timestamp
                        GlobalSetting.EnumProcessTimeStamp = System.Environment.TickCount;

                        //Enumerate all processes and find the processes by name and ID
                        Process[] allProcesses = Process.GetProcesses();

                        if (allProcesses != null)
                        {
                            foreach (Process proc in allProcesses)
                            {
                                if (proc.ProcessName.ToLower() == TargetProcessName.ToLower())
                                {
                                    bool inTheList = false;
                                    foreach (AutoAccount auto in AllAutoAccounts)
                                    {
                                        if (auto.Target.ProcessID == proc.Id)
                                        {
                                            inTheList = true;
                                            break;
                                        }
                                    }

                                    //If the process is not in the list yet, add to the list
                                    if (inTheList == false)
                                    {
                                        //Lock used by tmerUpdateUI as well
                                        lock (frmMain.frmMainInstance.GUILocker)
                                        {
                                            AutoAccount newAccount = new AutoAccount();
                                            newAccount.Target.ProcessID = proc.Id;
                                            newAccount.Target.Status = AllEnums.InjectionStatus.FoundProcess;

                                            //Add the new account into the list
                                            AllAutoAccounts.Add(newAccount);
                                        }
                                    }
                                } //if processName matching
                            } //foreach allProcesses
                        } //if allProcess != null
                    } //check enum process delay


                    //ReadProcessMemory routines
                    if (AllAutoAccounts.Count > 0)
                    {
                        foreach (AutoAccount account in AllAutoAccounts)
                        {
                            //ReadProcessMemory --> delegate into Task


                        } //foreach
                    } //if
                    //-- end of ReadProcessMemory


                    Thread.Sleep(GlobalSetting.ReadInfoDelay);
                } //if UIAvailable
            }
        }

        /// <summary>
        /// Download base addresses from the web page and save it to here
        /// </summary>
        /// <returns>TRUE nếu download thành công. FALSE nếu download thất bại</returns>
        public bool DownloadBaseAddress(string targetVersion)
        {
            MessageBox.Show("Download base addrseses từ server dựa trên version của target hiện tại");


            return true;
        }

    }
}
